<?php
class NodeHelper
{
	/**
	 * 显示node列表
	 */
	public static function getList($where=array(), $order='node_id DESC', $proto=null, $attrs=null, $s=0, $l=20)
	{
		$fields	=	($proto)?'t0.*':'*';
		$joins	=	array();
		$i	=	0;
		
		if($proto)
		{
			$i++;
			$joins[]	=	'Proto';
			$on[]		=	"t0.pro_id=t{$i}.pro_id";
			$fields		.=	",t{$i}.pro_title,t{$i}.pro_code";
		}
		
		$db	=	Db::get('Node')->select($fields);
		if($joins)
		{
			$db->join($joins)->on($on);
		}

		if(is_array($where) && isset($where['pro_id']))
		{
			$wp	= sprintf('t0.pro_id=%d', $where['pro_id']);
			unset($where['pro_id']);
			$list	=	$db->where($where, $wp)->order($order)->limit($s,$l)->getAll();
		}
		else
		{
			$list	=	$db->where($where)->order($order)->limit($s,$l)->getAll();
		}
		foreach($list as &$l)
		{
			$l['attrs']	=	unserialize($l['attrs']);
			$l['node_img']	=	$l['node_img'] ? explode(',', $l['node_img']) : array();
		}
		return $list;
	}
	
	/**
	 * 取一条完整的产品信息，含属性
	 *
	 * @param $id
	 * @return mixed false|array
	 */
	public static function load($id)
	{
		if(!intval($id))
		{
			return false;
		}
		
		$node	= Db::get('Node')->select()->where(array('_pk'=>$id))->getOne();
		if($node['attrs']){
			$node['attrs']	=	unserialize($node['attrs']);
		}
		$node['node_img']	=	$node['node_img'] ? explode(',', $node['node_img']) : array();
		
		return $node;
	}
	
}
?>